package com.example.imageexample.Main2;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.imageexample.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by Administrator on 2016/3/30.
 */
public class TypeImageLayout extends RelativeLayout {

    private LayoutInflater inflater;
    private View view;
    private Context mContext;

    private SimpleDraweeView simpleDraweeView;
    private ImageView imageView;

    public TypeImageLayout(Context context) {
        super(context);
        init(context);
    }

    public TypeImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.item_image, null);

        simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.simpleDraweeView);
        imageView = (ImageView) view.findViewById(R.id.img_hasgif_one);

        addView(view);
    }

    /**
     * 设置图片和类型
     * @param uri 图片的uri
     * @param type 图片的类型
     *             0 -- 不显示
     *             1 -- 长图
     *             2 -- gif
     */
    public void setImageAndType(String uri,int type){
        ImageRequest request1 = ImageRequestBuilder.newBuilderWithSource(getUriWithPath(uri))
                .setResizeOptions(new ResizeOptions(200, 200))
                .build();
        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setOldController(simpleDraweeView.getController())
                .setImageRequest(request1)
                .build();
        simpleDraweeView.setController(controller);

        switch (type){
            case 0:
                imageView.setVisibility(View.GONE);
                break;
            case 1:

                break;
            case 2:
                imageView.setVisibility(View.VISIBLE);
                imageView.setImageResource(R.mipmap.po_gif);
                break;
        }
    }

    public final String GIFSTRING = "/format/jpg";
    private Uri getUriWithPath(String filepath) {
        if (filepath.contains("file://")) {
            filepath = filepath.replace("file://", "");
        }
        Uri uri;
        if (filepath.startsWith("/storage/")) {
            uri = Uri.parse("file://" + mContext.getPackageName() + "/" + filepath);
        } else if (filepath.contains(".gif")){
            uri = Uri.parse(filepath + GIFSTRING);
        } else {
            uri = Uri.parse("" + filepath);
        }
        Log.e("uri","path:"+uri.getPath());
        return uri;
    }
}
